//
//  CPSPanPanGestureSelectView.m
//  CollTest
//
//  Created by codew on 1/4/22.
//

#import "CPSPanPanGestureSelectView.h"

@interface CPSPanPanGestureSelectView ()

@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIView *viewTitle;
@property (weak, nonatomic) IBOutlet UIView *viewContent;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentViewBottomConstraint;
@property (assign, nonatomic) CGFloat contentViewBottom;

@end

@implementation CPSPanPanGestureSelectView

+ (instancetype)viewPanGestureSelect
{
    CPSPanPanGestureSelectView *view = [[[NSBundle mainBundle] loadNibNamed:@"CPSPanPanGestureSelectView" owner:nil options:nil] firstObject];
    return view;
}


+ (void)showViewDemo
{
    CPSPanPanGestureSelectView *view = [CPSPanPanGestureSelectView viewPanGestureSelect];
    view.backgroundColor = [UIColor clearColor];
    view.frame = [UIScreen mainScreen].bounds;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    [view show];
}


- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    /*
     kCALayerMinXMinYCorner  // 左上角
     kCALayerMaxXMinYCorner  // 右上角
     
     kCALayerMinXMaxYCorner  // 左下角
     kCALayerMaxXMaxYCorner  // 右下角
     */
    self.viewContent.layer.cornerRadius = 14;
    self.viewContent.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
    self.viewContent.layer.masksToBounds = YES;
    
    UIPanGestureRecognizer *panPgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPressGestureRecongizerClick:)];
    [self.viewTitle addGestureRecognizer:panPgr];
}

- (void)panPressGestureRecongizerClick:(UIPanGestureRecognizer *)panPgr {
    CGPoint point = [panPgr translationInView:self.viewTitle];
    if (panPgr.state == UIGestureRecognizerStateBegan) {
        CGFloat contentViewBottom = self.contentViewBottom - point.y;
        if (contentViewBottom > 0) {
            contentViewBottom = 0;
        }
        self.contentViewBottomConstraint.constant = contentViewBottom;
    }else if (panPgr.state == UIGestureRecognizerStateChanged) {
        CGFloat contentViewBottom = self.contentViewBottom - point.y;
        if (contentViewBottom > 0) {
            contentViewBottom = 0;
        }
        self.contentViewBottomConstraint.constant = contentViewBottom;
    }else if (panPgr.state == UIGestureRecognizerStateEnded ||
              panPgr.state == UIGestureRecognizerStateCancelled) {
        if (self.contentViewBottomConstraint.constant < -100.f) {
            [self hide];
        }else {
            [UIView animateWithDuration:0.15 animations:^{
                self.contentViewBottomConstraint.constant = 0;
                [self layoutIfNeeded];
            }];
        }
        self.contentViewBottom = 0;
    }
}

- (void)show {
    [self layoutIfNeeded];
    [UIView animateWithDuration:0.25 animations:^{
        self.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        self.contentViewBottomConstraint.constant = 0;
        [self layoutIfNeeded];
    }];
}

- (void)hide {
    [UIView animateWithDuration:0.25 animations:^{
        
        self.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        self.contentViewBottomConstraint.constant = -350;
        [self layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
    }];
}

- (void)dealloc
{
    NSLog(@"%s, %@", __func__ , self);
}
@end
